package com.haitaoit.chengquan.module.home.activity.maintenance.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.home.model.FacilityMainenanceModel;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/10/24.
 */
public class FacilityMainenanceAdapter extends BaseAdapter {
    private Context                       context;
    private BackCall                      backCall;
    private List<FacilityMainenanceModel> list;
    private int                           type;
    private int IP = 0;

    public void setType(int type) {
        this.type = type;
    }

    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public FacilityMainenanceAdapter(Context context) {
        this.context = context;
    }

    public List<FacilityMainenanceModel> getList() {
        return list;
    }

    public void setList(List<FacilityMainenanceModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.macility_mainenance_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (type == 1) {
            holder.mainenance_tv.setVisibility(View.GONE);
            holder.offer_end_time.setText(list.get(position).getDescribe());
            String type = list.get(position).getType();

            if ("7".equals(type)) {
                holder.reference_price.setText("¥ " + list.get(position).getReference_price_low() +
                        "～¥ " + list.get(position).getReference_price_high() + " 元");
            } else {
                holder.reference_price.setText("¥ " + list.get(position).getReference_price_low() + " 元");
            }
        } else {
            holder.mainenance_tv.setVisibility(View.VISIBLE);

            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                String offer_end_time = list.get(position).getOffer_end_time();
                if (offer_end_time != null) {
                    StringBuffer buffer = new StringBuffer();
                    buffer.append(offer_end_time).append(":00");
                    Date d1 = dateFormat.parse(buffer.toString());
                    String format = dateFormat.format(new Date());
                    Date d2 = dateFormat.parse(format);
                    if (d1.getTime() > d2.getTime()) {
                        holder.items.setVisibility(View.VISIBLE);
                    }else {
                        holder.items.setVisibility(View.GONE);
                    }

                }

            } catch (ParseException e) {
                e.printStackTrace();
            }


            holder.offer_end_time.setText("截止时间：" + list.get(position).getOffer_end_time());
            holder.reference_price.setText("¥ " + list.get(position).getReference_price() + " 元");
        }
        ImageLoader.getInstance().displayImage(list.get(position).getTitle_img(), holder.title_img);
        holder.title.setText(list.get(position).getTitle());
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.items)
        LinearLayout items;
        @ViewInject(R.id.mainenance_tv)
        TextView     mainenance_tv;
        @ViewInject(R.id.title_img)
        ImageView    title_img;
        @ViewInject(R.id.title)
        TextView     title;
        @ViewInject(R.id.offer_end_time)
        TextView     offer_end_time;
        @ViewInject(R.id.reference_price)
        TextView     reference_price;
    }
}
